/*
https://leetcode.cn/problems/balanced-binary-tree/submissions/584021938/
110. 平衡二叉树
*/
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    bool isBalanced(TreeNode* root) {
        if(root==NULL) return true;
        int len1=maxDepth(root->left);
        int len2=maxDepth(root->right);
        if(abs(len1-len2)<=1) return isBalanced(root->left)&&isBalanced(root->right);
        return false;
    }
    int maxDepth(TreeNode* root) {
        if(root==NULL) return 0;
        int len1=maxDepth(root->left);
        int len2=maxDepth(root->right);
        return max(len1,len2)+1;
    }
};